﻿using Anley.DomainDrivenDesign.Contract.Extension;
using Anley.DomainDrivenDesign.Contract.Helper;

namespace Anley.DomainDrivenDesign.Contract.Infrastructure;

/// <summary>
/// 缓存仓储提供程序类
/// </summary>
public class RedisRepositoryContextProvider
    : IRedisRepositoryContextProvider
{
    private static string _connectionStringName = AneConsts.DefaultRedisConnectionStringName;

    /// <summary>
    /// 缓存连接字符串名称
    /// </summary>
    public string ConnectionStringName => _connectionStringName;

    /// <summary>
    /// 设置缓存连接字符串名称
    /// </summary>
    /// <param name="connectionStringName"></param>
    public static void SetConnectionStringName(
        string connectionStringName)
    {
        _connectionStringName = connectionStringName;
    }

    // 获取仓储上下文
    public IRedisRepositoryContext GetRepositoryContext()
    {
        var configuration = ServiceProviderHelper.ServiceProvider.GetRequiredService<IConfiguration>();
        var connectionString = configuration.GetConnectionString(ConnectionStringName);
        return new RedisRepositoryContext(connectionString.NotNullOrWhiteSpace(nameof(connectionString)));
    }
}